//
//  SYS_MemberDetailVC.m
//  System_App
//
//  Created by czy001 on 2017/12/4.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_MemberDetailVC.h"
#import "SYS_MemberDetailCell.h"
#import "SYS_UserInfoModel.h"
#import "SYS_SuitIconAndTextView.h"

@interface SYS_MemberDetailVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView *headImageView;
    UIImageView *userBgImageView;
}
@property (nonatomic, strong) DSNewTableView *mainTableView;
@property (nonatomic, strong) SYS_UserInfoModel *userModel;

@property (nonatomic,strong) UIImageView *iconImageView;
@property (nonatomic,strong) UILabel *nameLabel;
@end


@implementation SYS_MemberDetailVC

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
}

//在页面消失的时候就让出现
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden=NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=DGHColor(242, 242, 242);
    [self setNavigationBar];
}

#pragma mark -- setter and getter
- (void)setNavigationBar{
    
    headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150)];
    headImageView.image=[UIImage imageNamed:@"message_userHeadBg"];
    headImageView.userInteractionEnabled=YES;
    [self.view addSubview:headImageView];
    
    
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    leftBtn.frame=CGRectMake(15, 10+StatusBarHeight, 11, 22);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"SYS_Public_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backToPrevious) forControlEvents:UIControlEventTouchUpInside];
    [headImageView addSubview:leftBtn];
    
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 12+StatusBarHeight, SCREENWIDTH-60, 20)];
    titleLabel.textColor=DSCOLOR_WHITE;
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.font=[UIFont fontWithName:@"PingFangSC-Medium" size:18];
    titleLabel.text=@"用户信息";
    [headImageView addSubview:titleLabel];
    
    userBgImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(titleLabel.frame)+20, SCREENWIDTH-20, 160)];
    userBgImageView.userInteractionEnabled=YES;
    userBgImageView.image=[UIImage imageNamed:@"message_userBg"];
    [self.view addSubview:userBgImageView];
    
    _iconImageView=[UIImageView new];
    _iconImageView.layer.masksToBounds=YES;
    _iconImageView.layer.cornerRadius=22.5;
    
    _nameLabel=[[UILabel alloc]init];
    _nameLabel.textColor=DGHColor(100, 100, 100);
    _nameLabel.textAlignment=NSTextAlignmentCenter;
    _nameLabel.font=[UIFont fontWithName:@"PingFangSC-Regular" size:12];
    
    [userBgImageView sd_addSubviews:@[_iconImageView,_nameLabel]];
    
    _iconImageView.sd_layout
    .leftSpaceToView(userBgImageView, (SCREENWIDTH-65)/2)
    .topSpaceToView(userBgImageView, 15)
    .widthIs(45)
    .heightIs(45);

    _nameLabel.sd_layout
    .leftSpaceToView(userBgImageView, 10)
    .topSpaceToView(_iconImageView, 5)
    .widthIs(SCREENWIDTH-40)
    .heightIs(15);
    
    SYS_SuitIconAndTextView *phoneView=[[SYS_SuitIconAndTextView alloc]initWithFrame:CGRectMake(0, 80,(SCREENWIDTH-40)/2 , 60)];
    phoneView.iconImageView.image=[UIImage imageNamed:@"message_bodadh"];
    phoneView.nameLabel.text=@"拨打电话";
    phoneView.userInteractionEnabled=YES;
    phoneView.isAdjustPosition=YES;
    UITapGestureRecognizer *ges=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(phoneViewToPhone)];
    [phoneView addGestureRecognizer:ges];
    [userBgImageView addSubview:phoneView];
    
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake((SCREENWIDTH-20)/2, 100, 0.5, 30)];
    lineView.backgroundColor=DGHColor(221, 221, 221);
    [userBgImageView addSubview:lineView];
    
    SYS_SuitIconAndTextView *sendMessageView=[[SYS_SuitIconAndTextView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lineView.frame)+1, 80,(SCREENWIDTH-40)/2 , 40)];
    sendMessageView.isAdjustIconPosittion=YES;
    sendMessageView.iconImageView.image=[UIImage imageNamed:@"message_fasongxx"];
    sendMessageView.nameLabel.text=@"发送信息";
    sendMessageView.userInteractionEnabled=YES;
    UITapGestureRecognizer *ges1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sendMessageToSend)];
    [sendMessageView addGestureRecognizer:ges1];
    [userBgImageView addSubview:sendMessageView];
    
    
    _mainTableView=[[DSNewTableView alloc] initWithFrame:CGRectMake(13, CGRectGetMaxY(userBgImageView.frame)+10, SCREENWIDTH-26, SCREENHEIGHT-80) style:UITableViewStylePlain];
    _mainTableView.layer.masksToBounds=YES;
    _mainTableView.layer.cornerRadius=5;
    _mainTableView.delegate=self;
    _mainTableView.dataSource=self;
    _mainTableView.backgroundColor=DGHColor(242, 242, 242);
    _mainTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _mainTableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_mainTableView];
    //请求用户信息
    _userModel=[SYS_UserInfoModel new];
    [self requestDetailData];
}


#pragma mark   ----------拨打电话 和 发送信息-------------
-(void) phoneViewToPhone
{
    NSLog(@"拨打电话");
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_userModel.phone];
//    UIWebView * callWebview = [[UIWebView alloc] init];
//    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    [self.view addSubview:callWebview];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

}

-(void) sendMessageToSend
{
    NSLog(@"发送信息");
    ChatViewController *chatController = [[ChatViewController alloc] initWithConversationChatter:_userModel.imKey conversationType:EMConversationTypeChat];
    chatController.title = _userModel.nickName;
    
    DSDataBaseOperation *_operation=[[DSDataBaseOperation alloc]init];
    DGHUserInfo *info=[_operation searchUserInfoWithHXName:_userModel.imKey];
    if (!(info.headpic || info.username)) {
        EMMessage *mes=[[EMMessage alloc] initWithConversationID:nil from:_userModel.imKey to:_userModel.imKey body:nil ext:@{@"name":_userModel.nickName,@"image":_userModel.userLogo}];
        [_operation insertUserInfo:mes];
    }
    
    [self.navigationController pushViewController:chatController animated:YES];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setupUnreadMessageCount" object:nil];
}


#pragma mark   ----------请求用户信息-------------
-(void) requestDetailData
{
    NSString *session=[SYS_LoginModel instance].sessionInfo.session;
    UICKeyChainStore *keychainStore = [UICKeyChainStore keyChainStore];
    NSString *pid = [keychainStore stringForKey:@"pid"];
    NSDictionary *parma=@{@"session":session,@"userId":_userId,@"pid":pid};
    [self showHudInView:self.view hint:@"正在加载..."];
    [SYS_HttpUtility getMemberInfoRequest:parma success:^(id returnValue) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([returnValue[@"flag"] isEqualToString:@"true"]) {
            NSDictionary *dic=returnValue[@"data"];
            _userModel=[SYS_UserInfoModel mj_objectWithKeyValues:dic];
            /**************  头部视图信息  ************/
            [_iconImageView sd_setImageWithURL:[NSURL URLWithString:_userModel.userLogo] placeholderImage:[UIImage imageNamed:@"public_people"]];
            _nameLabel.text=_userModel.nickName;
            
            [_mainTableView reloadData];
        }else{
            [MBProgressHUD showSuccess:returnValue[@"exp"] toView:self.view];
        }
    } failure:^{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        RequestFailure;
    }];
}

#pragma mark   ----------UITableViewDelegate,UITableViewDataSource-------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYS_MemberDetailCell *cell = [[SYS_MemberDetailCell alloc]initWithTableView:tableView reuseId:@"SYS_MemberDetailCell"];
    cell.isSetFrame=YES;
    if (indexPath.row==0) {
        cell.iconImageView.image=[UIImage imageNamed:@"message_nicheng"];
        cell.nameLabel.text=@"昵称";
        cell.explainLabel.text=_userModel.nickName;
    }else if (indexPath.row==1){
        cell.iconImageView.image=[UIImage imageNamed:@"message_xingbie"];
        cell.nameLabel.text=@"性别";
        if ([_userModel.sex isEqualToString:@"0"]) {
            cell.explainLabel.text=@"女";
        }else{
            cell.explainLabel.text=@"男";
        }
    }else if (indexPath.row==2){
        cell.iconImageView.image=[UIImage imageNamed:@"message_shoujihh"];
        cell.nameLabel.text=@"手机号码";
        cell.explainLabel.text=_userModel.phone;
    }else if (indexPath.row==3){
        cell.iconImageView.image=[UIImage imageNamed:@"message_dizhi"];
        cell.nameLabel.text=@"详细地址";
        cell.explainLabel.text=_userModel.addr;
    }else if (indexPath.row==4){
        cell.iconImageView.image=[UIImage imageNamed:@"message_chushengny"];
        cell.nameLabel.text=@"出生年月日";
        if (![_userModel.birthday isEqualToString:@"0"]&&_userModel.birthday.length>0) {
            long long time=[_userModel.birthday longLongValue]/1000;
            cell.explainLabel.text=FORMAT(@"%@",[SYS_HelpModel timestampTransformedIntoStandardTimeYear:time]);
        }
    }else if (indexPath.row==5){
        cell.iconImageView.image=[UIImage imageNamed:@"message_bdmm"];
        cell.nameLabel.text=@"绑定门店";
        cell.explainLabel.text=_userModel.storeName;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (void)backToPrevious
{
    [self.navigationController popViewControllerAnimated:YES];
}



@end
